import { defineStore } from "pinia"

const state = () => ({
    sidebarStatus: {
        opened: !+!+localStorage.getItem('sidebarStatus'),
        withoutAnimation: false
    },
    device: 'desktop',
    uploadFileLists:[],

})

export const useAppStore = defineStore({
    id: 'app',
    state,
    actions: {
        INIT_UPLOAD(obj){
            const index = this.uploadFileLists.findIndex(item=>obj.fileId ===item.fileId)
            if(index === -1){
                this.uploadFileLists.push(obj)
            }
        },
        UPDATE_UPLOAD(obj){
            const index = this.uploadFileLists.findIndex(item=>obj.fileId ===item.fileId)
            if(index !== -1){
                this.uploadFileLists[index].progress = obj.progress
                this.uploadFileLists[index].isUploaded = obj.isUploaded
                this.uploadFileLists[index].total = obj.total
                this.uploadFileLists[index].loaded = obj.loaded
            }
            console.log(this.uploadFileLists)
        },
        CLEAR_UPLOAD(obj){
            this.uploadFileLists=obj
        },

        ToggleSideBar() {
            if (this.sidebarStatus.opened) {
                localStorage.setItem('sidebarStatus', 1)
            } else {
                localStorage.setItem('sidebarStatus', 0)
            }
            this.sidebarStatus.opened = !this.sidebarStatus.opened
        },
        CloseSideBar({ withoutAnimation }) {
            localStorage.setItem('sidebarStatus', 1)
            this.sidebarStatus.opened = false
            this.sidebarStatus.withoutAnimation = withoutAnimation
        }
    }
})
